
angular.module("MapModule", []);
angular
		.module("MapModule")
		.factory(
				"Map",
				function($http) {
					var map = null;
					var userLocation = null;
					var service = {};

					service.initMap = function(containerName) {
						var mapOptions = {
							panControl : true,
							zoomControl : true
						// scaleControl: true
						};
						var map = new google.maps.Map(document
								.getElementById(containerName), mapOptions);

						return map;
					};

					function handleNoGeolocation(errorFlag) {
						var content = null;
						if (errorFlag) {
							content = 'Error: The Geolocation service failed.';
						} else {
							content = 'Error: Your browser doesn\'t support geolocation.';
						}

						var options = {
							map : map,
							position : new google.maps.LatLng(60, 105),
							content : content
						};

						new google.maps.InfoWindow(options);
						map.setCenter(options.position);
					}

					service.locateMe = function(callback) {
						if (navigator.geolocation) {
							var r = {};
							navigator.geolocation.getCurrentPosition(function(
									position) {
								var pos = new google.maps.LatLng(
										position.coords.latitude,
										position.coords.longitude);
								r.ob = pos.ob;
								r.pb = pos.pb;
								map.setCenter(pos);
								map.setZoom(15);

								if (userLocation != null)
									userLocation.setMap(null);

								userLocation = new google.maps.Marker({
									position : pos,
									map : map,
									animation : google.maps.Animation.BOUNCE,
									title : 'You are here',
									icon : '/common/img/you_are_here.png'
								});
								setTimeout(function() {
									userLocation.setAnimation(null);
								}, 2000);
								if (callback)
									callback(pos);
							}, function() {
								handleNoGeolocation(true);
							});
							return r;
						} else {
							// Browser doesn't support Geolocation
							handleNoGeolocation(false);
						}
					};

					service.setCenter = function(lat, lng) {
						var pos = new google.maps.LatLng(lat, lng);
						map.setCenter(pos);
					}

					service.zoom = function(zoom) {
						map.setZoom(zoom);
					};

					service.getAddress = function(lat, long, success, error,
							finallly) {
						var r = {};
						$http
								.get(
										"http://maps.googleapis.com/maps/api/geocode/json?latlng="
												+ lat + "," + long
												+ "&sensor=true")
								.then(
										function(data, a, b, c, d) {
											r.address = data.data.results[0].formatted_address;
											if (success)
												success(r, a, b, c, d);
										}, error, finallly);
						return r;
					};

					// Add a marker to the map and push to the array.
					service.addMarker = function(location, title, icon) {
						var marker = new google.maps.Marker({
							position : location,
							map : map,
							animation : google.maps.Animation.DROP,
							title : title,
							icon : icon
						});
						return marker;
					};

					service.removeFeature = function(marker) {
						marker.setMap(null);
					};

					service.addCircle = function(map, center, radius, editable,
							color) {
						if (color == null)
							color = '#0000FF';
						if (editable == null)
							editable = false;
						var options = {
							strokeColor : color,
							strokeOpacity : 0.6,
							strokeWeight : 1,
							fillColor : color,
							fillOpacity : 0.05,
							map : map,
							center : center,
							radius : radius,
							editable : editable
						};

						return new google.maps.Circle(options);
					};

					service.addListener = function(feature, event, fun) {
						google.maps.event.addListener(feature, event, fun);
					};
					return service;
				});